package com.ruijie.acl.web;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

import com.ruijie.acl.AccessGroup;
import com.ruijie.acl.AclPlanInterface;
import com.ruijie.acl.TimeInfo;
import com.ruijie.acl.TimeRange;
import com.ruijie.acl.service.AceService;
import com.ruijie.acl.service.TimeInfoService;
import com.ruijie.acl.service.TimeRangeService;
import com.ruijie.acl.task.TaskAclUtil;
import com.ruijie.foundation.bo.EntityObject;
import com.ruijie.foundation.service.Repository;

/**
 * <p>
 * Title: ACL计划管理中摘要指令查看
 * 
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * ACL计划管理中摘要指令查看
 * 
 * </p>
 * <p>
 * Copyright: Copyright (c) 2008
 * </p>
 * <p>
 * Company: Ruijie Co., Ltd.
 * </p>
 * <p>
 * Create Time:
 * </p>
 * <p>
 * </p>
 * <p>
 * Update Time:
 * </p>
 * <p>
 * Updater:
 * </p>
 * <p>
 * Update Comments:
 * </p>
 * 
 * @author ASAI
 */
@Name("commandAction")
@Scope(ScopeType.CONVERSATION)
public class CommandAction extends AclBaseAction {

	private List<AclPlanInterface> aclPlanInterfaces;
	private List<AccessGroup> accessGroups;
	// "planList" 计划列表
	// "planInterfaceList" 计划接口列表
	private String enterType;
	private String msg = null;

	@In("#{aceService}")
	private AceService aceService;
	@In("#{timeRangeService}")
	private TimeRangeService timeRangeService;
	@In("#{timeInfoService}")
	private TimeInfoService timeInfoService;
 
	public String toDetailPage(String enterType,List<AccessGroup> accessGroups,
			List<AclPlanInterface> aclPlanInterfaces) {
		this.enterType=enterType;
		this.accessGroups = accessGroups;
		this.aclPlanInterfaces = aclPlanInterfaces;
		return "/acl/plan/components/commandDetail.seam";
	}

	public List<AclPlanInterface> getAclPlanInterfaces() {
		return aclPlanInterfaces;
	}

	public void setAclPlanInterfaces(List<AclPlanInterface> aclPlanInterfaces) {
		this.aclPlanInterfaces = aclPlanInterfaces;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	/**
	 * 获取指令的详细信息
	 * 
	 * @return
	 */
	public String getCommandDetailMsg() {
		StringBuffer interfaceMsg = new StringBuffer();
		StringBuffer accessGroupMsg = new StringBuffer();
		StringBuffer timeRangeMsg = new StringBuffer();
		Set<TimeRange> timeRanges = new HashSet<TimeRange>();
		
		//添加接口信息
		if(aclPlanInterfaces!=null){
			for (AclPlanInterface aclPlanInterface : aclPlanInterfaces) {
				interfaceMsg.append(TaskAclUtil
						.aclPlanInterface2String(aclPlanInterface));
			}
		}
		if("planList".equals(this.enterType)){
			//ACL部署
			if(!accessGroups.isEmpty()){
				for (AccessGroup accessGroup : accessGroups) {
					accessGroup.setAces(this.aceService.getByAccessGroup(accessGroup));
					accessGroupMsg.append(TaskAclUtil.accessGroup2String(accessGroup));
					timeRanges.addAll(timeRangeService.findByAccessGroup(accessGroup));
				}
			}
			
			//添加时间段信息
			if (timeRanges!=null&&!timeRanges.isEmpty()) {
				for (TimeRange timeRange : timeRanges) {
					if (timeRange.getTimeInfos() == null) {
						// 获取时间段中的时间信息
						List<TimeInfo> infos = this.timeInfoService
								.getByTimeRange(timeRange);
						Set<TimeInfo> sets = new HashSet<TimeInfo>();
						sets.addAll(infos);
						timeRange.setTimeInfos(sets);
					}
					timeRangeMsg.append(TaskAclUtil.timeRange2String(timeRange));
				}
			}
		}
		
		return timeRangeMsg.toString() + accessGroupMsg.toString()
				+ interfaceMsg.toString();
	}

	@SuppressWarnings("unchecked")
	@Override
	protected Repository getRepository() {
		return null;
	}

	@Override
	protected String getAddPage() {
		return null;
	}

	@Override
	public EntityObject getCriteria() {
		return null;
	}

	@Override
	protected String getDetailPage() {
		return null;
	}

	@Override
	protected String getListPage() {
		return null;
	}

	@Override
	public EntityObject getModel() {
		return null;
	}

	@Override
	protected String getUpdatePage() {
		return null;
	}

	@Override
	public void resetModel() {

	}

	@Override
	public void setCriteria(EntityObject criteria) {

	}

	@Override
	public void setModel(EntityObject model) {

	}
	
	public String toDetailPageBlank(String enterType,List<AccessGroup> accessGroups,
			List<AclPlanInterface> aclPlanInterfaces){
		this.toDetailPage(enterType, accessGroups, aclPlanInterfaces);
		return null;
	}

}
